package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.mongo.UserLocation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class UserLocationApiImpl implements UserLocationApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /*上报(更新)地理位置*/
    @Override
    public Boolean updateLocation(Long userId, Double longitude, Double latitude, String address) {
        try {
            //查询用户地理位置信息
            Query query = Query.query(Criteria.where("userId").is(userId));
            UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);
            //如果不存在则进行保存
            if (userLocation == null) {
                userLocation = new UserLocation();
                userLocation.setUserId(userId);
                userLocation.setAddress(address);
                userLocation.setCreated(System.currentTimeMillis());
                userLocation.setUpdated(System.currentTimeMillis());
                userLocation.setLastUpdated(System.currentTimeMillis());
                userLocation.setLocation(new GeoJsonPoint(longitude, latitude));
                mongoTemplate.save(userLocation);
            } else {
                //存在则进行更新
                Update update = Update.update("location", new GeoJsonPoint(longitude, latitude))
                        .set("updated", System.currentTimeMillis())
                        .set("lastUpdated", userLocation.getUpdated());
                mongoTemplate.updateFirst(query, update, UserLocation.class);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    /*查寻附近用户*/
    @Override
    public List<Long> searchNearUser(Long userId, Double metre) {
        //构造查询条件
        Criteria criteria = Criteria.where("userId").is(userId);
        Query query = Query.query(criteria);
        //查询当前用户的位置信息
        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);
        //判断是否存在
        if (userLocation == null) {
            return null;
        }
        //以当前用户的位置为原点
        GeoJsonPoint point = userLocation.getLocation();
        //绘制半径
        Distance distance = new Distance(metre / 1000, Metrics.KILOMETERS);
        //绘制圆形
        Circle circle = new Circle(point, distance);
        //查询
        Query locationQuery = Query.query(Criteria.where("location").withinSphere(circle));
        List<UserLocation> list = mongoTemplate.find(locationQuery, UserLocation.class);
        //提取数据列表中所有的用户Id
        List<Long> ids = CollUtil.getFieldValues(list, "userId", Long.class);
        return ids;
    }
}
